package com.alibaba.shopping_user_customer_api.controller;

import com.alibaba.shopping_common.pojo.ShoppingUser;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.MessageService;
import com.alibaba.shopping_common.service.ShoppingUserService;
import com.alibaba.shopping_common.utils.RandomUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ShoppingUserController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/16 19:36
 */
@RestController
@RequestMapping("/shoppingUser")
public class ShoppingUserController {

    @DubboReference
    private MessageService messageService;

    @DubboReference
    private ShoppingUserService shoppingUserService;

    /**
     * 发送注册验证码
     * @param phone
     * @return
     */
    @GetMapping("/sendMessage")
    public BaseResult registerCheckCode(String phone){

        // 1. 生成6为随机验证码
        String code = RandomUtil.generateCode(6);
        // 2. 给用户发送短信
        BaseResult baseResult = messageService.sendMessage(phone, code);
        if (baseResult.getCode() == 200){
            // 3. 将验证码和手机号写入Redis缓存
            shoppingUserService.saveRegisterCheckCode(phone,code);
            return BaseResult.OK();
        }else {
            System.out.println("baseResult:"+baseResult);
            return baseResult;
        }
    }

    @GetMapping("/registerCheckCode")
    public BaseResult registerCheck(String phone,String checkCode){
        // 1.校验验证码和手机号
        shoppingUserService.registerCheckCode(phone,checkCode);
        // 2.没有异常，直接返回
        return BaseResult.OK();
    }

    /**
     * 注册控制器
     * @param shoppingUser 用户提交的注册表单
     * @return 注册结果
     */
    @PostMapping("/register")
    public BaseResult<ShoppingUser> register(@RequestBody ShoppingUser shoppingUser){
        System.out.println("shoppingUser:"+shoppingUser);
        shoppingUserService.register(shoppingUser);
        return BaseResult.OK(shoppingUser);
    }

    /**
     * 登录-使用账号密码进行登录
     * @param shoppingUser 用户信息
     */
    @PostMapping("/loginPassword")
    public BaseResult<String> loginPassword(@RequestBody ShoppingUser shoppingUser){
        System.out.println("loginPassword方法"+shoppingUser);
        String sign = shoppingUserService.loginPassword(shoppingUser.getUsername(), shoppingUser.getPassword());
        return BaseResult.OK(sign);
    }

    @GetMapping("/sendLoginCheckCode")
    public BaseResult sendLoginCheckCode(String phone){
        // 1. 判断当前手机号是否已经注册
        shoppingUserService.checkUserRegister(phone);
        // 2. 生成随机的6为验证码
        String code = RandomUtil.generateCode(6);
        // 3. 发送短信
        BaseResult baseResult = messageService.sendMessage(phone, code);
        if (baseResult.getCode() == 200){
            // 写入Redis缓存
            shoppingUserService.saveLoginCheckCode(phone,code);
            return BaseResult.OK();
        }else {
            return baseResult;
        }
    }

    @PostMapping("/loginCheckCode")
    public BaseResult<String> loginCheckCode(String phone,String checkCode){
        // 1. 判断当前手机号是否已经注册
        shoppingUserService.checkUserRegister(phone);
        // 校验手机号和验证码
        String sign = shoppingUserService.loginCheckCode(phone, checkCode);
        return BaseResult.OK(sign);
    }

    @GetMapping("/getName")
    public BaseResult<String>getName(@RequestHeader("Authorization") String authorization){
        String token = authorization.replace("Bearer ","");
        String name = shoppingUserService.getName(token);
        return BaseResult.OK(name);
    }

}




